package com.yunsom.testdemo.multiple_thread.pipe_stream;

import java.io.ByteArrayOutputStream;
import java.io.PipedInputStream;

public class Receiver extends Thread{
    private PipedInputStream pis;

    public Receiver(PipedInputStream pis) {
        super();
        this.pis = pis;
    }

    public void run() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = pis.read(buf)) != -1){
                baos.write(buf, 0, len);
            }
            byte[] result = baos.toByteArray();
            String s = new String(result, 0, result.length);
            System.out.println("Receiver receive : " + s);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
